package hb.watcher.monitor;


import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Statement;


import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;


public class CharsActivity extends TabActivity {
	
	public int nToonId;
	
	public DBConnection dbconn;
	public List<ChatTabs> ChatTabsList = new ArrayList<ChatTabs>();
	
	private boolean initComplete = false;
	private String statement;
	private String toonname;
	TabHost tabH;

	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        this.nToonId = extras.getInt("toonId");
        
        this.toonname = extras.getString("toonname");
      
    	dbconn = (DBConnection)getIntent().getSerializableExtra("dbConn");
    	
		// construct the TAB Host
        tabH= new TabHost(this);
        
       

        LinearLayout lin= new LinearLayout(this);
        lin.setOrientation(LinearLayout.VERTICAL);

        // the tabhost needs a tabwidget, that is a container for the visible tabs
        TabWidget tabWidget = new TabWidget(this);
        tabWidget.setId(android.R.id.tabs);
        tabH.addView(tabWidget);
        tabH.addView(lin);

        // the tabhost needs a frame layout for the views associated with each visible tab
        FrameLayout fl = new FrameLayout(this);
        fl.setId(android.R.id.tabcontent);
        fl.setPadding(0, 65, 0, 0);
        lin.addView(fl); 

        //tabH.getTabWidget().setStripEnabled(false);
        tabH.setup();
        
        
        dbconn.Register(this);
        

        
		//Updater upt = new Updater();
		//upt.execute();
		
	    
    }
	
	private boolean notiState = false;
	
	private void SetNotificationState(boolean state)
	{
		notiState = state;
	}
	public boolean GetNotificationState()
	{
		return notiState;
	}
	public boolean working = false;
	
	public boolean InitDone()
	{
		return initComplete;
	}
	
	public String getStatement()
	{
		return statement;
	}
	public void Init()
	{
		statement = "SELECT `sub`.`szMessageType`, `sub`.`szMessagePartner` FROM ( SELECT `szMessageType` , CASE `szMessageType` WHEN 'GUILD_MESSAGE' THEN 'Guild' WHEN 'YELL_MESSAGE' THEN 'Yell' WHEN 'SAY_MESSAGE' THEN 'Say' WHEN 'BATTLEGROUND_MESSAGE' THEN 'Bg' WHEN 'PARTY_MESSAGE' THEN 'Party' ELSE `szMessagePartner` END AS szMessagePartner FROM `TABMessageReceive`  WHERE `nToonId` = " + nToonId + " ) `sub` group by `sub`.`szMessageType`, `sub`.`szMessagePartner` ";	
		ChatTabsList.add(new ChatTabs(nToonId,"SAY_MESSAGE", "Say"));
		ChatTabsList.add(new ChatTabs(nToonId,"YELL_MESSAGE", "Yell"));
		
		initComplete = true;
	}
	
	public void Update(ResultSet rs)
	{
		try
		{
			
			while (rs.next()) {
				
				boolean bfound = false;
				
				String MessageType = rs.getString("szMessageType");
				String MessagePartner = rs.getString("szMessagePartner");
				
				for(int i = 0; i < ChatTabsList.size(); i++)
				{
					if(ChatTabsList.get(i).getChatType().equals(MessageType) && ChatTabsList.get(i).getHeader().equals(MessagePartner))
					{
						bfound = true;
						break;
					}
						
				}
				
				if(!bfound)
				{
					ChatTabs chars = new ChatTabs(nToonId,MessageType, MessagePartner);
					ChatTabsList.add(chars);
				}
				
			}
			
			if(!working)
			{
				Updater upt = new Updater();
				upt.execute();
				working = true;
			}
			
			rs.close();
			
			
		}catch(Exception ex)
		{
			
		}
		
	}
	
	private class Updater extends AsyncTask<Void, Void, Void>
	{
		private String Tag = "";
		private boolean AddTabs(int Toonid, String MessageType, String MessagePartner)
		{
			try
			{
		    TabHost tabhost = getTabHost();
			
			Intent intent;
			tabhost.setup();

			Bundle params = new Bundle();
			params.putInt("Toonid", nToonId);
			params.putString("MessageType", MessageType);
			params.putString("MessagePartner", MessagePartner);
			params.putString("toonname", toonname);
			params.putSerializable("dbConn", dbconn);
			
			
			intent = new Intent().setClass(getBaseContext(), ChatActivity.class);
		    intent.putExtras(params);
		    
		    
		    TabHost.TabSpec spec = tabhost.newTabSpec(MessagePartner).setIndicator(MessagePartner)
	          .setContent(intent);
		        
		    
		    tabhost.addTab(spec);
		    String tag = spec.getTag();
		    Tag = tag;
		    tabhost.setCurrentTabByTag(spec.getTag());
		    tabhost.refreshDrawableState();
		   
		/*    Class c = intent.getClass();
		   	Method me = c.getDeclaredMethod("Register");
		   	me.invoke(null,null);*/
		   
			return true;
			
			}catch(Exception ex)
			{
				return false;
			}
		}

		List<ChatTabs> ChatTabsList_temp;
		
		@Override
		protected Void doInBackground(Void... params) {
			
			ChatTabsList_temp = ChatTabsList;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				working = false;
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			
			try
			{
			
			TabHost tabhost = getTabHost();
				
			for(int i = 0; i < ChatTabsList_temp.size(); i++)
			{
				if(!ChatTabsList_temp.get(i).TabCreated()){
				
					boolean created = AddTabs(ChatTabsList_temp.get(i).getToonId(), ChatTabsList_temp.get(i).getChatType(), ChatTabsList_temp.get(i).getHeader());
					ChatTabsList_temp.get(i).TabCreated(true);					
				}
					
				
				
			}
			
			}catch(Exception ex)
			{
				
			}
			working = false;
			
		}
		
		
	}
	
	
}
